package dmxGroupControl.listeners;

import javax.sound.midi.ShortMessage;

import dmxGroupControl.Universe;

public class MidiMessageMapping extends Mapping
{
	int pattern;
	
	public MidiMessageMapping(Universe universe, int pattern, String attribute)
	{
		super(universe, attribute);
		this.pattern = pattern;
	}
	
	@Override
	public boolean Matches(Object matchTo)
	{
		if(matchTo instanceof ShortMessage)
		{
			ShortMessage match = (ShortMessage)matchTo;
			int command = match.getCommand();
			if(command == 0xb0)
			{
				return pattern == match.getData1();
			}
		}
		return false;
	}

	@Override
	public String GetPattern()
	{
		return pattern + "";
	}

	@Override
	public void SetPattern(String value)
	{
		try
		{
			pattern = Integer.parseInt(value);
		}
		catch (NumberFormatException e)
		{
            e.printStackTrace();
		}
	}

	@Override
	public int GetGroup(Object matchTo, int groupOffset)
	{
		ShortMessage match = (ShortMessage)matchTo;
		return match.getChannel();
	}

	@Override
	public float GetValue(Object matchTo)
	{
		ShortMessage match = (ShortMessage)matchTo;
		int intValue = match.getData2();
		return ((float)intValue) / 127.0f;
	}
}
